
var timer = null;
import store from "../store"
var xtTimer=null
export default {
    start: function () {
        if (!store.state.isLogin) {
            return 
        }
        const url = process.env.NODE_ENV == 'development' ?"wss://socket.weile999.com":"wss://socket.weile999.com"
        window.socket = new WebSocket(url);
        window.socket.onopen = function () {
        
            if (store.state.isLogin) {
                let param = JSON.stringify({ channel: 'auth', data: { token: store.state.token, content: "auth" } });
                window.socket.send(param);
            }
            if(xtTimer){
                clearInterval(xtTimer)
                xtTimer=null
            }
            xtTimer=setInterval(()=>{
                let param = JSON.stringify({ channel: 'ping'});
                window.socket.send(param);
            },1000*30)
        };

        window.socket.addEventListener("close", () => {
            if (!store.state.isLogin) {
                return 
            }
            reconnect(url)
        });
        window.socket.addEventListener("error", () => {
            if (!store.state.isLogin) {
                return 
            }
            reconnect(url)
        });

        // 重连
        function reconnect(url) {
            clearInterval(xtTimer)
            xtTimer=null
            clearTimeout(timer)
            timer = null;
            timer = setTimeout(function () {     //没连接上会一直重连，设置延迟避免请求过多
                createWebSocket(url);
            }, 2000);
        }

        // 实例websocket
        function createWebSocket(url) {
            try {
                if ('WebSocket' in window) {
                    window.socket = new WebSocket(url);
                } else if ('MozWebSocket' in window) {
                    window.socket = new MozWebSocket(url);
                } else {
                    _alert("当前浏览器不支持websocket协议,建议使用现代浏览器", 3000)
                }
                initEventHandle();
            } catch (e) {
                reconnect(url);
            }
        }

        // 初始化事件函数
        function initEventHandle() {
            window.socket.addEventListener("close", () => {
                if (!store.state.isLogin) {
                    return 
                }
                reconnect(url)
            });
            window.socket.addEventListener("error", () => {
                if (!store.state.isLogin) {
                    return 
                }
                reconnect(url)
            });
            window.socket.onopen = function () {
             
                if (store.state.isLogin) {
                    let param = JSON.stringify({ channel: 'auth', data: { token: store.state.token, content: "auth" } });
                    window.socket.send(param);
                }
                if(xtTimer){
                    clearInterval(xtTimer)
                    xtTimer=null
                }
                xtTimer=setInterval(()=>{
                    let param = JSON.stringify({ channel: 'ping'});
                    window.socket.send(param);
                },1000*30)
            };

        }
    }
}
